layui.define(['jquery','element'], function(exports) {
	"use strict";
	var $ = layui.jquery;
	var element = layui.element;

	var FilterManager = {
		visGraph : null,
		filterTypes : {
			entityType : {
				id:"entityType",
				label:"实体类型过滤"
			},
			linkType : {
				id:"linkType",
				label:"关系类型过滤"
			}
      // ,
			// infoFilter : {
			// 	id:"infoFilter",
			// 	label:"信息内容过滤"
			// }
		},

		init : function(wrapEleId,visGraph){
			this.visGraph = visGraph;

			var item_template =  '<div class="layui-collapse">';
			for(var type in this.filterTypes){
				item_template += '<div class="layui-colla-item">'+
								    '<h2 class="layui-colla-title">'+this.filterTypes[type]['label']+'</h2>'+
								    '<div class="layui-colla-content layui-show"><div id="'+this.filterTypes[type]['id']+'" class="filter-div"></div></div>'+
								  '</div>';
			}
			item_template += '</div>';

			$(wrapEleId).append(item_template);

			this.getFilterParams();
			element.render();
		},

		getFilterParams : function(){
			this.getEntityTypes();
			this.getLinkTypes();
			// this.getInfoFilter();
		},

		getEntityTypes : function(){
			var _self = this;
			var nodeTypes = [];
			_self.visGraph.nodes.forEach(function(n){
				// var type = String(n.type);
        let type = typeMap[n.type]
				if(type != null && type && !nodeTypes.filter(d=>d.value == n.type).length){
					nodeTypes.push({
            label:type,
            value:n.type
          });
				}
			});

			$("#entityType").empty();
			var nodeTypesLength = nodeTypes.length;
			if(nodeTypesLength > 0){
				var entity_template = '<ul>';
				for(var i=0; i<nodeTypesLength; i++){
					entity_template +='<li>'
									 +'<label>'+nodeTypes[i].label+'</label>'
									 +'<input type="checkbox" checked="true" nodetype="'+nodeTypes[i].value+'"></li>';
				}
				entity_template += '</ul>';
				$("#entityType").html(entity_template);
			}

			$('#entityType ul > li > input[type="checkbox"]').on('click',function(){
				_self.getFilterCondition();
			});
		},

		getLinkTypes : function(){
			var _self = this;
			var linkTypes = [];
			_self.visGraph.links.forEach(function(l){
				if(l.label != null && l.label && linkTypes.indexOf(l.label) == -1){
					linkTypes.push(l.label);
				}
			});

			$("#linkType").empty();
			var linkTypesLength = linkTypes.length;
			if(linkTypesLength > 0){
				var link_template = '<ul>';
				for(var i=0; i<linkTypesLength; i++){
					link_template +='<li><label>'+linkTypes[i]+'</label>'
					+'<input type="checkbox" checked="true" linktype="'+linkTypes[i]+'"></li>';
				}
				link_template += '</ul>';
				$("#linkType").append(link_template);
			}else{
				$("#linkType").append('<div class="nodata-tip">暂无数据...</div>');
			}

			$('#linkType ul > li > input[type="checkbox"]').on('click',function(){
				_self.getFilterCondition();
			});
		},

		getInfoFilter : function(){
			var _self = this;
			var info_template = '<ul>';
			info_template += '<li><label>实体的度</label>'
					        +'<select class="filterSelect">'
					        +'<option value="1">大于</option>'
					        +'<option value="2">等于</option>'
					        +'<option value="3">小于</option>'
					        +'</select>'
					        +'<input type="number" class="form-control" value="" placeholder="输入值">'
					        +'<a class="layui-icon layui-icon-filter" id="linkCount"></a>'
					        +'</li>';

			info_template += '<li><label>实体出度</label>'
							+'<select class="filterSelect">'
							+'<option value="1">大于</option>'
							+'<option value="2">等于</option>'
							+'<option value="3">小于</option>'
							+'</select>'
							+'<input type="number" class="form-control" value="" placeholder="输入值">'
							+'<a class="layui-icon layui-icon-filter" id="linkOut"></a>'
							+'</li>';

			info_template += '<li><label>实体入度</label>'
							+'<select class="filterSelect">'
							+'<option value="1">大于</option>'
							+'<option value="2">等于</option>'
							+'<option value="3">小于</option>'
							+'</select>'
							+'<input type="number" class="form-control" value="" placeholder="输入值">'
							+'<a class="layui-icon layui-icon-filter" id="linkIn"></a>'
							+'</li>';

			info_template += '<li id=""><label>关系权重</label>'
					        +'<select class="filterSelect">'
					        +'<option value="1">大于</option>'
					        +'<option value="2">等于</option>'
					        +'<option value="3">小于</option>'
					        +'</select>'
					        +'<input type="number" class="form-control" value="" placeholder="输入值">'
					        +'<a class="layui-icon layui-icon-filter" id="linkWeight"></a>'
					        +'</li>';

			info_template += '<li><label>实体名称</label>'
					        +'<select class="filterSelect">'
					        +'<option value="1">等于</option>'
					        +'<option value="2">模糊</option>'
					        +'</select>'
					        +'<input type="text" class="form-control" placeholder="输入内容"/>'
					        +'<a class="layui-icon layui-icon-filter" id="entityDesc"></a>'
					        +'</li>';

			$("#infoFilter").html(info_template);

			$("#infoFilter a.layui-icon").on("click",function(){
				_self.getFilterCondition();
			});
		},

		getFilterCondition : function(){
			var entityFalse = [];
			$('#entityType ul > li').each(function(a){
				var _input = $(this).find('input[type="checkbox"]');
				if(!_input[0].checked){
					entityFalse.push(+_input.attr('nodeType'));
				}
			});

			var linkFalse = [];
			$('#linkType ul > li').each(function(a){
				var _input = $(this).find('input[type="checkbox"]');
				if(!_input[0].checked){
					linkFalse.push(_input.attr('linkType'));
				}
			});

			var infoFilterParams = {};
			$('#infoFilter ul > li').each(function(a){
				var _search = $(this).find('a.layui-icon');
				var _id = _search.attr("id");
				var _compare = _search.prev().prev()[0].value;
				var _input = _search.prev()[0].value;
				if(_input != ''){
					infoFilterParams[_id] = [];
					infoFilterParams[_id].push(_compare);
					infoFilterParams[_id].push(_input);
				}
			});
			this.visGraph.filterChangeVisible(entityFalse,linkFalse,infoFilterParams);
		}
	};
	exports("GraphVisFilter", FilterManager);
});
